<?php
/**
 * Created by PhpStorm.
 * User: Cloud
 * Date: 2018/4/8
 * Time: 9:09
 */

class Member_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
        $this->load->library('session');
        $this->load->model('bill_model');
        $this->load->library('message');
    }

    //用户修改接口
    public function  company_user_edit($post_arr){
        $url=$this->config->item("shop_url").'companymanagerapi/company_user_edit/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
        $data_arr['uid'] = $post_arr['shop_user_id'];
        if (isset($post_arr['tel'])){
            $data_arr['mobile'] = $post_arr['tel'];
        }
        if (isset($post_arr['name'])){
            $data_arr['name'] = $post_arr['name'];
        }
        if (isset($post_arr['email'])){
            $data_arr['email'] = $post_arr['email'];
        }
        if (!empty($post_arr['password'])){
            $data_arr['password'] = $post_arr['password'];
        }
        $rs=curl_request($url,$data_arr);
        $rs = json_decode($rs,true);
        if ($rs['code'] == 200){
            return $rs;
        }else{
            return false;
        }
    }

    //发邮箱验证码
    public function admin_edit_email(){
        $customer_id=$this->session->customer_id;
        $post=$this->input->post();
        //邮箱验证
        if (!empty($post['email'])){
            $data['email'] = $post['email'];
            send_json(array('code' => 0, 'data' => '', 'msg' => '发送失败'));
            $sender_name = 666666;//发邮件人的地址
            $code = generate_code(6);//验证码
            $to_email = $data['email'];//收件人 email 地址
            $title = '你的验证码';// email 主题:
            $content = "【文具1号企业站】您的验证码为" . $code . "，请勿向他人泄露验证码。如非本人操作，请忽略。"; //email内容
            $res = $this->bill_model->send_email($sender_name, $to_email, $title, $content, $mailtype = 'html');//发送email
            if ($res == true) {
                $this->message->add_msg_session($data['email'], $code);
                //   log_message('error', '发送邮件成功，内容为：' . $content);
                send_json(array('code' => 0, 'data' => '', 'msg' => "发送成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => '发送失败'));
            }
        }

    }

    //手机验证码
    public function admin_edit_tel(){
        $post=$this->input->post();
        if (!empty($post['tel'])){
            $data['tel']=$post['tel'];
            $code = generate_code(6);
            $msg = "【文具1号企业站】您的验证码为".$code."，请勿向他人泄露验证码。如非本人操作，请忽略。";
            $res = $this->message->send_msg( $data['tel'],$msg);
            if($res['code'] == 0)
            {
                $this->message->add_msg_session($post['tel'],$code);
                send_json(array('code'=>0,'data'=>'','msg'=>"success"));
            }else{
                send_json(array('code'=>-1,'data'=>'','msg'=>$res['msg']));
            }
        }

    }
    // 添加/修改商城发票抬头接口
    public function company_user_invoice($post_arr){
        $url=$this->config->item("shop_url").'companymanagerapi/company_user_invoice/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
        $data_arr['type'] = $post_arr['invoice_type'];
        $data_arr['uid'] = $post_arr['shop_user_id'];

        //  2代表增值发票   4代表企业普通发票
        if ($data_arr['type']==4){
            if (isset($post_arr['shop_id'])){
                $data_arr['shop_id'] = $post_arr['shop_id'];
            }
            $data_arr['title'] = $post_arr['invoice_company_name'];
            if (isset($post_arr['invoice_content'])){
                $data_arr['content'] = $post_arr['invoice_content'];
            }else{
                $data_arr['content'] = 1;
            }
            $data_arr['pcode'] = $post_arr['invoice_code'];
        }
        if ($data_arr['type']==2){
           if (isset($post_arr['shop_id'])){
               $data_arr['shop_id'] = $post_arr['shop_id'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['company'] = $post_arr['invoice_company_name'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['address'] = $post_arr['invoice_address'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['code'] = $post_arr['invoice_code'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['tel'] = $post_arr['invoice_tel'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['bank'] = $post_arr['invoice_bank'];
           }
           if (isset($post_arr['invoice_company_name'])){
               $data_arr['card'] = $post_arr['invoice_account'];
           }
       }
       $rs=curl_request($url,$data_arr);
       $rs = json_decode($rs,true);
       return $rs;

   }
   //默认地址接口
   public function company_user_address_default($post_arr){
    $url=$this->config->item("shop_url").'companymanagerapi/company_user_address_default/';
    $data_arr['time'] = time();
    $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
    $data_arr['user_id'] = $post_arr['shop_user_id'];
    $data_arr['addr_id'] = $post_arr['shop_id'];
    $rs=curl_request($url,$data_arr);
    $rs = json_decode($rs,true);
    if ($rs['code'] == 200){
        return $rs;
    }else{
        return false;
    }

}
//新增/修改商城地址的接口
public function company_user_address($post_arr){
    $url=$this->config->item("shop_url").'companymanagerapi/company_user_address/';
    $data_arr['time'] = time();
    $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
    $data_arr['user_id'] = $post_arr['shop_user_id'];
    $data_arr['consignee'] = $post_arr['name'];
    $data_arr['zipcode'] = $post_arr['postcode'];//邮编
    $data_arr['region_id'] = $post_arr['region_id'];
    $data_arr['region_name'] = $post_arr['region_name'];
    $data_arr['address'] = $post_arr['address'];
    if(isset($post_arr['tel'])){
        $data_arr['phone_tel'] = $post_arr['tel'];
    }
    if(isset($post_arr['phone'])){
        $data_arr['phone_mob'] = $post_arr['phone'];
    }
    if(isset($post_arr['shop_id'])){
        $data_arr['addrId'] = $post_arr['shop_id'];
    }
    $rs=curl_request($url,$data_arr);
    $rs = json_decode($rs,true);
    if ($rs['code'] == 200){
      return $rs['data'];
  }else{
    return false;
}
}

//多地址判断商品配送范围和邮费
public function multiple_address_postage_scope($post_arr)
{
    $url=$this->config->item("shop_url").'companymanagerapi/getMoreAdressPostage/';
    $data_arr['time'] = time();
    $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
    $data_arr['shop_user_id'] = $post_arr['shop_user_id'];
    $data_arr['list'] = $post_arr['list'];
    $rs=curl_request($url,$data_arr);
    $rs = json_decode($rs,true);
    if ($rs['code'] == 200){
      return $rs['data'];
  }else{
    return false;
}
}

//多地址提交订单到商城
public function creat_moreorder_to_mall($post_arr)
{
    $url=$this->config->item("shop_url").'companymanagerapi/CreatMoreOrder/';
    $data_arr['time'] = time();
    $data_arr['key'] = md5($this->config->item("token_const_wjyh").$data_arr['time']);
    $data_arr['buyerId'] = $post_arr['shop_user_id'];
    $data_arr['list'] = $post_arr['list'];
    $data_arr['invoice_id'] = $post_arr['invoice_id'];
    $data_arr['type'] = $post_arr['type'];
    $data_arr['invoice_cont'] = $post_arr['invoice_cont'];
    $rs=curl_request($url,$data_arr);
    $rs = json_decode($rs,true);
    
    if ($rs['code'] == 200){
        return $rs['data'];
    }else{
        return false;
    }
}

}